#ifndef PINENTITY_H
#define PINENTITY_H

#include "../ecs/entity.h"

enum PinDirection{
    Up,
    Down,
    Left,
    Right
};

class PinEntity : public Entity
{
    Q_OBJECT

public:
    PinEntity(QGraphicsItem* parent = nullptr);
    QRectF boundingRect() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;

    const int& pinMode(){return mPinMode;}
    const int& pinVal(){return mPinVal;}
    void resetState();

    const QColor& pinInColor() { return mPinInColor; }
    void setPinInColor(const QColor& color) { mPinInColor = color; }
    const QColor& pinOutColor() { return mPinOutColor; }
    void setPinOutColor(const QColor& color) { mPinOutColor = color; }
    const QColor& pinGroundColor() { return mPinGroundColor; }
    void setPinGroundColor(const QColor& color) { mPinGroundColor = color; update(); }
    const PinDirection& pinDirection() { return mDirection; }
    void setPinDirection(const PinDirection& direction) { mDirection = direction; }
    bool isConnected() const { return mIsConnected; }
    void setConnected(const bool& connected) { mIsConnected = connected; }
    QList<int> currentNodes() const { return mCurrentNodes.toList(); }
    void addCurrentNode(const int& id);
    void removeCurrentNode(const int& id);
    QList<int> connectors() const { return mConnectors.toList(); }
    void addConnector(const int& id);
    void removeConnector(const int& id);
    qreal volt() const { return mVolt; }
    void setVolt(const qreal& v) { mVolt = v; }
    bool isGround() const { return mPinMode == 2; }

public slots:
    void setPinVal(const int& val);
    void setPinMode(const int& mode);


signals:
    void pinValChanged(const int& val);
    void pinModeChanged(const int& mode);

protected:

private:
    int mPinMode; // -1: UNSET, 0: INPUT, 1: OUTPUT, 2: GROUND
    int mPinVal;
    qreal mVolt;
    QColor mPinInColor;
    QColor mPinOutColor;
    QColor mPinGroundColor;
    PinDirection mDirection;
    bool mIsConnected;
    QSet<int> mCurrentNodes;
    QSet<int> mConnectors;
};

#endif // PINENTITY_H
